//
//  PublicViewController.m
//  TestApp
//
//  Created by 杨志涛 on 15/9/1.
//  Copyright (c) 2015年 杨志涛. All rights reserved.
//
#import "PublicViewController.h"


#define GIF_WIDTH    240
#define GIF_HEIGHT   240

@interface PublicViewController ()
{
    UIWebView *ProgressGIF;
}


@end

@implementation PublicViewController
@synthesize userDefaults;
@synthesize isShowProgressHUD;
@synthesize isShowProgressGIF;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = COLOR_BGView;
    
    userDefaults = [NSUserDefaults standardUserDefaults];
    parseJson =[[ParseJson alloc]init];
    
    [self settingTitleTextAttributes];


    
    /*
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        //屏幕像四周延伸方式
        self.edgesForExtendedLayout = UIRectEdgeNone;
        //自动调整滚动视图
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
     */
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        //屏幕像四周延伸方式
        self.edgesForExtendedLayout = UIRectEdgeAll;
        //自动调整滚动视图
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    self.tabBarController.tabBar.hidden = YES;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)handleSwipe:(UISwipeGestureRecognizer *)sender{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)showMessage:(NSString *)string time:(float)delay{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.labelText = string;
    hud.margin = 10.f;
    hud.yOffset = 0.0f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:delay];
}

- (void)showProgressHUD{
    isShowProgressHUD = YES;
    
    //全局APP loading
    //[[AppModel sharedInstance] showAppProgressHUD];
    //return;
    
    //原有代码
    progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:progressHUD];
    
    [progressHUD setLabelFont:APP_FONT_SUBTITLE];
//    [progressHUD setLabelText:@"正在加载"];
    progressHUD.dimBackground = NO;
    // Regiser for HUD callbacks so we can remove it from the window at the right time
    progressHUD.delegate = self;
    // Show the HUD while the provided method executes in a new thread
    [progressHUD show:YES];
}

- (void)showProgressHUD:(NSString*)msg
{
    isShowProgressHUD = YES;
    
    if (progressHUD == nil) {
        progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:progressHUD];
    }
    
    [progressHUD setLabelFont:APP_FONT_BUTTON];
    [progressHUD setLabelText:msg];
    progressHUD.dimBackground = NO;
    // Regiser for HUD callbacks so we can remove it from the window at the right time
    progressHUD.delegate = self;
    // Show the HUD while the provided method executes in a new thread
    [progressHUD show:YES];
}

- (void)hideProgressHUD{
    isShowProgressHUD = NO;
    
    //全局APP loading
    //[[AppModel sharedInstance] hideAppProgressHUD];
    //return;
    
    [progressHUD hide:YES];
}
-(void)showProgressGIF:(NSData*)gifData
{
    isShowProgressGIF = YES;
    
    CGRect frame = CGRectMake((SCREEN_WIDTH-GIF_WIDTH)/2, (SCREEN_HEIGHT-GIF_HEIGHT)/2, GIF_WIDTH, GIF_HEIGHT);
    
    UIImage *aimage = [UIImage imageWithData: gifData];
    CGSize size = aimage.size;
    
    if (size.width < 320) {
        frame = CGRectMake((SCREEN_WIDTH-size.width)/2, (SCREEN_HEIGHT-size.height)/2, size.width, size.height);
    }
    else if (size.width >= 320)
    {
        float height =  (320/size.width) * size.height;
        frame = CGRectMake(0, (SCREEN_HEIGHT-height)/2, 320, height);
    }
    
    if (ProgressGIF == nil) {
        ProgressGIF = [[UIWebView alloc] init];
        [self.view addSubview:ProgressGIF];
    }
    
    // view生成
    [ProgressGIF setFrame:frame];
    [ProgressGIF setOpaque:NO];
    [ProgressGIF setBackgroundColor:[UIColor clearColor]];
    [ProgressGIF loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [ProgressGIF setHidden:NO];
    
}

-(void)hideProgressGIF
{
    isShowProgressGIF = NO;
    
    [ProgressGIF setHidden:YES];
}


- (void)hudWasHidden:(MBProgressHUD *)hud {
    if (progressHUD) {
        isShowProgressHUD = NO;
        [progressHUD removeFromSuperview];
        progressHUD.delegate = nil;
        progressHUD = nil;
    }
}


#pragma UIAlertView method

- (void)showAlert:(NSString *)message{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:message
                          delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil
                          ];
    [alert show];
}

- (void)showAlertSelect:(NSString *)message :(int)tag{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:message
                          delegate:self
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:@"取消",nil
                          ];
    alert.tag = tag;
    [alert show];
}

- (void)showAlertForSelf:(NSString *)message :(int)tag{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:message
                          delegate:self
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil
                          ];
    alert.tag = tag;
    [alert show];
}
- (void)showAlertSelectWithTitle:(NSString *)message title:(NSString*)title
                         leftBtn:(NSString*)leftBtn  rightBtn:(NSString*)rightBtn :(int)tag{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:title
                          message:message
                          delegate:self
                          cancelButtonTitle:leftBtn
                          otherButtonTitles:rightBtn,nil
                          ];
    alert.tag = tag;
    [alert show];
}
//带输入框
- (void)showAlertWithTextField:(NSString *)message title:(NSString*)title
                       leftBtn:(NSString*)leftBtn  rightBtn:(NSString*)rightBtn :(int)tag{
    
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:title
                          message:message
                          delegate:self
                          cancelButtonTitle:leftBtn
                          otherButtonTitles:rightBtn,nil];
    alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
    alert.tag = tag;
    
    [alert show];
}


#pragma  mark - LoginOut
-(void)loginOut{
    [userDefaults setBool:false forKey:IS_LOGIN];
    [userDefaults setObject:nil forKey:USER_ID];
    [userDefaults setObject:nil forKey:LOGIN_PASS];
    [userDefaults setObject:nil forKey:USER_NMAE];
    [userDefaults setObject:nil forKey:USER_PHONE];
    [userDefaults setObject:nil forKey:USER_EMAIL];
    [userDefaults setObject:nil forKey:USER_PhoneChecked];
    [userDefaults setObject:nil forKey:USER_EmailChecked];
    [userDefaults setObject:nil forKey:USER_RegDate];
    [userDefaults setObject:nil forKey:USER_LastLoginDate];
    [userDefaults setObject:nil forKey:USER_RegIP];
    [userDefaults setObject:nil forKey:USER_LastLoginIP];
    [userDefaults setObject:nil forKey:USER_UserState];
    [userDefaults setObject:nil forKey:USER_UserType];
    [userDefaults synchronize];//保存设置
}

/*
 * 公共解析成功与否判断字符
 */
-(BOOL)requestResult:(NSDictionary*)dictionary
{
    
    return [[dictionary objectForKey:@"returnResult"] boolValue];
}
/*
 * 公共解析成功返回的msg
 */
-(NSString*)requestMsg:(NSDictionary*)dictionary
{
    return [dictionary objectForKey:@"returnMessage"];
}


/*
 * 设置标题栏的字体大小
 */
-(void)settingTitleTextAttributes{
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,[UIFont systemFontOfSize:18],UITextAttributeFont, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
}


/*
 * 验证码倒计时正常状态
 */
- (UIButton*)DownTimeBtnNormal:(UIButton*)codebtn
{
    UIButton * btn = [[UIButton alloc]init];
    btn = codebtn;
    
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:UIMainVIPColor forState:UIControlStateNormal];
    [btn setEnabled:YES];
    [btn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    
    return btn;
    
}
/*
 * 验证码倒计时动态显示状态
 */
- (UIButton*)DownTimeBtnGoon:(UIButton*)codebtn countSecond:(NSInteger)countSecond
{
    UIButton * btn = [[UIButton alloc]init];
    btn = codebtn;
    
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [btn setTitle:[NSString stringWithFormat:@"%ld秒后重新获取",TRANSFORM_TO_INTEGER(countSecond)] forState:UIControlStateNormal];

    return btn;
}

/*
 * 设置确定按钮样式的公用方法
 */
- (UIButton*)setSendBtn:(UIButton*)sendBtn
{
    UIButton * btn = [[UIButton alloc]init];
    btn = sendBtn;
    
    btn.frame = CGRectMake(sendBtn.frame.origin.x, sendBtn.frame.origin.y, sendBtn.frame.size.width, 42);
    btn.layer.cornerRadius = 4;
    btn.backgroundColor = UIMainVIPColor;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    return btn;
}

/*
 * 设置确定按钮样式的公用方法
 */
- (UIButton*)setSendBtn2:(UIButton*)sendBtn
{
    UIButton * btn = [[UIButton alloc]init];
    btn = sendBtn;
    
    btn.frame = CGRectMake(sendBtn.frame.origin.x, sendBtn.frame.origin.y, sendBtn.frame.size.width, 42);
    btn.layer.cornerRadius = 4;
    btn.layer.borderColor  = UIMainVIPColor.CGColor;
    btn.layer.borderWidth  = 0.8f;
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:UIMainVIPColor forState:UIControlStateNormal];
    
    
    return btn;
}




#pragma mark - 刷新





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
